<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Statistics_model extends CI_Model{
		
		function __construct()
		{
			parent::__construct();
            $this->platreg_table = 'platreg_log';
			$this->dic_table = 'dictionary';
			$this->platreg_percent = 0;//默认扣除百分比
			//$this->group = 'type';
		}

		public function get_platreg_list($dic_id,$time_start,$time_end)
	    {

            $days = array();
            while ($time_start <= $time_end){
                $days[] = date('Y-m-d',$time_end);
                $time_end -= 3600*24;
            }
            foreach ($days as $key=>$val){
                $list[] = $this->get_platreg_day($dic_id,$val);
            }
            return $list;
	    }

        public function get_platreg_day($dic_id,$day)
        {
            $dic_info = $this->db->query("SELECT * FROM tan_dictionary WHERE id=$dic_id")->row_array();
            $list = array();
            //day的零点和24点--
            $time_00 = strtotime($day);
            $time_24 = strtotime($day)+3600*24;
            //默认百分比
            $platreg_percent = $this->platreg_percent;
            //day当天注册的真实人数
            $day_real_sql = "select count(*) as num from tan_users where reg_plat={$dic_info['value']} and reg_time>$time_00 and reg_time<$time_24";
            $day_real_num = $this->db->query("{$day_real_sql}")->row_array();
            //day当天的记录信息
            $day_sql = "select * from tan_platreg_log where dic_id={$dic_info['id']} AND log_time = '$day'";
            $day_val = $this->db->query("{$day_sql}")->row_array();
            //day之前最后一天的记录信息
            $beforday_sql = "select * from tan_platreg_log where dic_id=$dic_id and log_time < '$day' order by log_time desc limit 1";
            $beforday_val = $this->db->query("{$beforday_sql}")->row_array();

            $list['dic_id'] = $dic_id;
            $list['name']   = $dic_info['name'];
            $list['value']   = $dic_info['value'];
            $list['day'] = $day;
            $list['real_num'] = $day_real_num['num'];
            //if(day当天的记录已经产生）
            if ($day_val){
                //if (day当天的end_num已经存在){则day已经计算完毕直接取值}else{用day当天的记录进行计算}
                if ($day_val['end_num']){
                    $list['percent_num'] = $day_val['end_num'];
                    $list['percent'] = $day_val['percent'];
                }else{
                    $list['percent_num'] = floor(((100-$day_val['percent'])/100)*($day_real_num['num']-$day_val['real_num'])+$day_val['per_num']);
                    $list['percent'] = $day_val['percent'];
                }
            }else{
                //if (day之前最后一天的记录信息存在){则day之前最后一天记录的percent计算}else{用默认percent进行计算}
                if ($beforday_val){
                    $list['percent_num'] = floor(((100-$beforday_val['percent'])/100)*$day_real_num['num']);
                    $list['percent'] = $beforday_val['percent'];
                }else{
                    $list['percent_num'] = floor((100-$platreg_percent)/100*$day_real_num['num']);
                    $list['percent'] = $platreg_percent;
                }
            }

            return $list;
        }


	}